clc,clear,close all
videoReader = vision.VideoFileReader('viptraffic.avi');
videoPlayer = vision.VideoPlayer;

frame = 110;

video_s = cell(1,frame);
diff_s = cell(1,frame);
idx = 0;
while ~isDone(videoReader)
    idx = idx + 1;
    videoFrame = videoReader();
    video_s{idx} = videoFrame;
    if idx>frame
        break;
    end
    videoPlayer(videoFrame);
    pause(0.1)
end

idx = 0;
while idx<frame
    idx = idx + 1;
    diff_s{idx} = video_s{idx+1}-video_s{idx};
%     videoPlayer(diff_s{idx});
%     pause(0.1);
end

% 使用diff的信息估计运动方向
idx = 0;
figure;
search_size = 30;


is_exist_panel = 1;
every_frame_first_roi = [0,0];
while idx<frame
    disp(strcat('frame:',num2str(idx)));
    idx = idx + 1;
    current_img = rgb2gray(diff_s{idx});
    current_img = imbinarize(current_img);
    img = diff_s{idx};
    next_img = rgb2gray(diff_s{idx+1});
    next_img = imbinarize(next_img);

    % roi点的搜索区域固定
    % 图像特征roi匹配
    if idx>1 && length(car_location)>1
        is_multi_car = 1;
    else
        is_multi_car = 0;
    end
    car_location = cell(1);
    idx_location = 1;
    record = 0;
    while is_exist_panel
        [x_current, y_current] = find(current_img);
        roi_current_x = round(mean(x_current));
        roi_current_y = round(mean(y_current));

        % 两个roi中，选择第一个点的roi的方式，不能通过全局计算
        if record==0 && is_multi_car
            if every_frame_first_roi(1)~=0
                [start_x,end_x,start_y,end_y] = get_location(current_img,every_frame_first_roi(2),every_frame_first_roi(1),search_size);
                roi = current_img(start_y:end_y,start_x:end_x);
                [x_first, y_first] = find(roi);
                if length(x_first)>5 % 如果上一帧第一个car存在则进行重定位，若不存在，第一点仍使用全局点
                    roi_current_y = start_x+round(mean(y_first));
                    roi_current_x = start_y+round(mean(x_first));
                end
            end
        end
        
        if length(x_current)>10
            car_location{idx_location} = [roi_current_y,roi_current_x];
            idx_location = idx_location+1;
            % de-roi
            de_roi = current_img;
        
            [start_x,end_x,start_y,end_y] = get_location(current_img,roi_current_x,roi_current_y,search_size);
            de_roi(start_y:end_y,start_x:end_x) = 0;

            current_img = de_roi; % 后续继续寻找是否存在roi
        else
            break;
        end
        
        record = record+1;
        if record>5
            break;
        end
    end
    
    imshow(img,[]);
    for i = 1:length(car_location)
        if car_location{i}~=0
            hold on,plot(car_location{i}(1), car_location{i}(2),'r*');
        end
    end
    
    pause(0.1);
    every_frame_first_roi = car_location{1};
end

edge = video_s{1}-video_s{2};

figure,imshow(edge,[]);
disp('1');


function [start_x,end_x,start_y,end_y] = get_location(current_img,roi_current_x,roi_current_y,search_size)
    start_x = roi_current_y-search_size;
    end_x = roi_current_y+search_size;
    start_y = roi_current_x-search_size;
    end_y =roi_current_x+search_size;
    if start_x<=0
        start_x = 1;
    end
    if start_y<=0
        start_y = 1;
    end
    if end_x>=size(current_img,2)
        end_x = size(current_img,2);
    end
    if end_y>=size(current_img,1)
        end_y = size(current_img,1);
    end
end
